package ig.metrica.ejb.conbalance;

import ig.util.ejb.IGBean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.naming.NamingException;

/**
 * This is the bean class for the ConfiguracionBalanceSBBean enterprise bean.
 * Created 12/12/2006 11:23:40 AM
 * @author jsanchez
 */
public class ConfiguracionBalanceSBBean  {
    
    public Collection findByModelo(Integer idModelo)  throws java.lang.Exception {
        Connection MetricaDW=null;
        PreparedStatement prep=null;
        Collection CC=new ArrayList();
        ResultSet res=null;
        try {
            MetricaDW=IGBean.getMetricaDW();//this.getConexion("METRICADW");
            
            prep=MetricaDW.prepareStatement("SELECT IDMODELO, PUC, DURACION, PROCENTAJE FROM CONFIGURACIONBALANCES WHERE IDMODELO = ?");
            prep.setInt(1,idModelo);
            res=prep.executeQuery();
            while(res.next()){
                CC.add(
                        new ConfiguracionbalancesDTO(
                        res.getInt("IDMODELO"),
                        res.getInt("PUC"),
                        res.getInt("DURACION"),
                        res.getInt("PROCENTAJE")
                        )
                        );
            }
            prep.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {prep.close();} catch (Exception e) {}
           // try {MetricaDW.close();} catch (Exception e) {}
            try {res.close();} catch (Exception e) {}
        }
        return CC;
    }
    public void crear(Collection c) throws java.lang.Exception {
        //TODO implement crear
//        boolean eliminar=true;
        Connection MetricaDW=null;
        PreparedStatement prep=null;
        try {
            MetricaDW=IGBean.getMetricaDW();//this.getConexion("METRICADW");
            for (Iterator it = c.iterator(); it.hasNext();) {
                ConfiguracionbalancesDTO elem = (ConfiguracionbalancesDTO) it.next();
//                if(eliminar){
//                    this.eliminar(elem.getIdmodelo());
//                    eliminar=false;
//                }
                prep=MetricaDW.prepareStatement("INSERT INTO CONFIGURACIONBALANCES(IDMODELO, PUC, DURACION, PROCENTAJE) VALUES(?, ?, ?, ?)");
                prep.setInt(1,elem.getIdmodelo());
                prep.setInt(2,elem.getPuc());
                prep.setInt(3,elem.getDuracion());
                prep.setInt(4,elem.getProcentaje());
                prep.executeUpdate();
                prep.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {prep.close();} catch (Exception e) {}
            //try {MetricaDW.close();} catch (Exception e) {}
        }
    }
    
    public void eliminar(Integer idModelo) throws java.lang.Exception {
        Connection MetricaDW=null;
        PreparedStatement prep=null;
        try {
            MetricaDW=IGBean.getMetricaDW();//this.getConexion("METRICADW");
            prep=MetricaDW.prepareStatement("DELETE FROM CONFIGURACIONBALANCES WHERE IDMODELO = ?");
            prep.setInt(1,idModelo);
            prep.executeUpdate();
            prep.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {prep.close();} catch (Exception e) {}
            //try {MetricaDW.close();} catch (Exception e) {}
        }
    }
    
}
